package com.weijie.relaxed.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.weijie.relaxed.App;
import com.weijie.relaxed.R;
import com.weijie.relaxed.net.Api;
import com.weijie.relaxed.net.StringListener;
import com.weijie.relaxed.views.InputView;

/** 
 * 注册
 * @author weijie 
 * @version 创建时间：2015年3月18日 下午4:02:58 
 */
public class RegisterActivity extends Activity implements OnClickListener {

	private InputView username,password,again; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		findViewById(R.id.back).setOnClickListener(this);
		findViewById(R.id.register).setOnClickListener(this);
		username = (InputView) findViewById(R.id.username);
		password = (InputView) findViewById(R.id.password);
		again = (InputView) findViewById(R.id.again);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.register:
			if(username.getText().trim().equals("")) {
				Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
				return;
			}
			if(password.getText().trim().equals("")) {
				Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
				return;
			}
			if(!again.getText().trim().equals(password.getText().trim())) {
				Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
				return;
			}
			Api.register((App)getApplication(), new Listener(),username.getText().trim(),password.getText().trim());
			break;

		default:
			break;
		}
	}
	
	class Listener extends StringListener {

		@Override
		public void onResponse(String response) {
			Toast.makeText(RegisterActivity.this, "成功", Toast.LENGTH_SHORT).show();
			finish();
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			Toast.makeText(RegisterActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
		}

	}

}
